#coding=utf-8
from DjangoBbs.BbsTheme.common import ThemeViewHelper
from PublicTool.ViewHelper import ViewHelper
import models,forms




__author__ = 'czq'
class LineView(ThemeViewHelper):
    def __init__(self):
        self.moduleName='Trave'
        self.typeName='line'
    def get_model(self):
        return models.BbsTheme



    def show_map(self,request,id):
        return super(LineView,self).show(request,id,url='show_map')

    def list(self,request):
        return super(LineView,self).list(request)

    def add_save_prev(self,request,instance,change):
        instance.addUser=request.user
        instance.classId=self.classModel
        return True
    def add_save_next(self,request,instance,change):
        formset=forms.RideLineFormSet(request.POST,instance=instance)
        if formset.is_valid():
            formset.save()
            #raise Exception,type(line[0])
        formset=forms.RidePositionFormSet(request.POST,instance=instance)
        if formset.is_valid():
                formset.save()
    def add_get_dict(self,request):
        dict={}
        #from DjangoBbs.BbsTheme.forms import BbsThemeForm
        #self.themeForm=BbsThemeForm
        #dict['themeForm']=self.themeForm
        dict['classModel']=self.classModel


        #if request.method=="POST":
        #    self.RideLineFormSet=forms.RideLineFormSet(request.POST,instance=)
        #    self.RidePositionFormSet=forms.RidePositionFormSet(request.POST)
        #else:
        if self.id!='0':
            instance=models.BbsTheme.objects.get(id=self.id)
            self.RideLineFormSet=forms.RideLineFormSet(instance=instance)
            self.RidePositionFormSet=forms.RidePositionFormSet(instance=instance)
        else:
            self.RideLineFormSet=forms.RideLineFormSet
            self.RidePositionFormSet=forms.RidePositionFormSet
        dict['RideLineFormSet']=self.RideLineFormSet
        dict['RidePositionFormSet']=self.RidePositionFormSet
        return dict
    def add_get_form(self,request):
        from DjangoBbs.BbsTheme.forms import BbsThemeForm
        return BbsThemeForm
    def add_response_succeed(self,request,dict,instance):
        return super(LineView,self).showUrl('%s%s/'%(instance.classId.typeId.urlShow,instance.id))#showUrlReverse('bbsTheme_show',args=(instance.id,))
    def add(self,request,id='0'):
        self.id=id
        classId=request.GET.get('classId','')
        if classId!='0':
            from DjangoBbs.BbsTheme.models import BbsClass
            self.classModel=BbsClass.objects.get(id=classId)
        return super(LineView,self).add(request,id)

    def show_get_dict(self,request,instance):
        dict={}
        #posList=models.RidePosition.objects.filter(travelLine=instance).order_by("rank")
        rideLine=models.RideLine.objects.get(theme=instance)
        posList=models.RidePosition.objects.filter(theme=instance).order_by('rank')
        dict['rideLine']=rideLine
        dict['posList']=posList
        return super(LineView,self).show_get_dict(request,instance,dict)
    def show(self,request,id):
        return super(LineView,self).show(request,id)



class PositionView(ViewHelper):
    def __init__(self):
        self.moduleName='TraveRecord'
        self.typeName='position'

    def get_model(self):
        return models.RidePosition


    def show(self,request,id):
        return super(PositionView,self).show(request,id)

